<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class Order
 * Desc  用户订单Model
 * Create on 2024/10/25 13:45
 * Create by wangyafang
 */

namespace app\api\model\wddj\user;


use think\Model;

class Order extends Model
{
// 表名
    protected $name = 'wddj_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    protected $type = [
        'actual_amount'     =>  'float',
        'pay_amount'     =>  'float',
    ];


    protected static function init()
    {
        self::beforeInsert(function ($row) {
            if ($row['type'] == 1) {
                $row->departure_time = self::getDepartureTime();
            }
        });
    }

    /**
     * Desc 获取当前出发时间
     * Create on 2024/10/25 16:49
     * Create by wangyafang
     */
    public static function getDepartureTime()
    {
        $currentYear = date('Y');
        $currentMonth = date('m');
        $currentDay = date('d');
        $currentHour = date('H');
        $currentMinute = date('i');

// Determine the starting and ending minutes for the range
        if ($currentMinute < 10) {
            $startMinute = 0; // Start at 00
            $endMinute = 10;  // End at 10
        } elseif ($currentMinute < 20) {
            $startMinute = 10; // Start at 10
            $endMinute = 20;   // End at 20
        } elseif ($currentMinute < 30) {
            $startMinute = 20; // Start at 20
            $endMinute = 30;   // End at 30
        } elseif ($currentMinute < 40) {
            $startMinute = 30; // Start at 30
            $endMinute = 40;   // End at 40
        } elseif ($currentMinute < 50) {
            $startMinute = 40; // Start at 40
            $endMinute = 50;   // End at 50
        } else {
            $startMinute = 50; // Start at 50
            $endMinute = 59;    // End at 59
        }

// Format the output
        $output = sprintf('%04d-%02d-%02d %02d:%02d-%02d:%02d',
            $currentYear,
            $currentMonth,
            $currentDay,
            $currentHour,
            $startMinute,
            $currentHour,
            $endMinute
        );

        return $output;
    }


    /**
     * Desc 待接单详情数据
     * Create on 2024/11/1 14:18
     * Create by wangyafang
     */
    public static function acceptanceDetails($orderObj)
    {
        $paymentsObj = (new Payments())->where('order_id',$orderObj['id'])->find();
        if (empty($paymentsObj['pay_time'])) {
            $paymentsObj['pay_time'] = time();
        }
        $front = $paymentsObj['pay_time'] + 120;
        $front_time = date('H:i',$front);

        $behind = $paymentsObj['pay_time'] + 720;
        $behind_time = date('H:i',$behind);
        return $front_time.'-'.$behind_time;
    }

    /**
     * Desc 订单详情获取乘车人信息
     * Create on 2024/11/5 9:14
     * Create by wangyafang
     */
    public static function getPassenger($id)
    {
        $passengerObj = (new Passenger())->get($id);
        $data = array();
        if ($passengerObj) {
            $data = array(
                'name'=>$passengerObj['name'],
                'mobile'=>substr_replace($passengerObj['mobile'], '*****', 3, -3),
            );
        }

        return $data;

    }
}



 